using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using Vin.Extension.Framework.Aop;
using Vin.Extension.Framework.Base;
using Vin.Extension.Framework.Constants;
using Vin.Extension.Framework.Monitor.Filters;
using Vin.Extension.Framework.Quartz.Dto.Entities;
using Vin.Extension.Framework.Quartz.Dto.List;
using Vin.Extension.Framework.Quartz.Jobs;
using Vin.Extension.Framework.Quartz.ServiceInterface;
using Vin.Tool.Attributes;
using Vin.Tool.Core.AssemblyCategory;

namespace Vin.Extension.Framework.Monitor.Controller.Controllers;

[Route("monitor/job")]
public class SysJobController : VinFrameworkBaseController
{
    [Inject] private readonly ISysJobService _sysJobService = null!;

    [Inject] private readonly ISysJobGroupService _sysJobGroupService = null!;
    
    [Inject] private readonly ISysJobRecordService _sysJobRecordService = null!;

    #region 任务区

    [VinAuthorize("monitor:job:list"), HttpGet("list")]
    public async Task<IActionResult> List([FromQuery] SysJobListDto dto)
    {
        return Success(await _sysJobService.ListAsync(dto), dto.TotalCount);
    }

    [VinAuthorize("monitor:job:query"), HttpGet("query")]
    public async Task<IActionResult> Query(long id)
    {
        return Success(await _sysJobService.QueryAsync(id));
    }

    [VinAuthorize("monitor:job:add,monitor:job:edit"), OperateLog("定时任务管理", VinBusinessType.InsertOrUpdate),
     HttpPost("save")]
    public async Task<IActionResult> Save([FromBody] SysJobDto dto)
    {
        return Success(await _sysJobService.SaveAsync(dto));
    }

    [VinAuthorize("monitor:job:del"), OperateLog("定时任务管理", VinBusinessType.Delete), HttpDelete("del")]
    public async Task<IActionResult> Del(long id)
    {
        return Success(await _sysJobService.DelAsync(id));
    }

    [VinAuthorize("monitor:job:query"), HttpGet("types")]
    public IActionResult List()
    {
        var types = AssemblyTool.GetTypes<IJob>().Where(m => m != typeof(VinBaseJob));
        var list = types.Select(m => new
        {
            Name = m.FullName,
            Params = m.GetProperties().Select(p => p.Name)
        });
        return Success(list);
    }

    [VinAuthorize("monitor:job:start"), OperateLog("定时任务管理", VinBusinessType.Update), HttpPost("start")]
    public async Task<IActionResult> Start([Required(ErrorMessage = "任务不能为空")] long? id)
    {
        return Success(await _sysJobService.StartAsync(id!.Value));
    }

    [VinAuthorize("monitor:job:stop"), OperateLog("定时任务管理", VinBusinessType.Update), HttpPost("stop")]
    public async Task<IActionResult> Pause([Required(ErrorMessage = "任务不能为空")] long? id)
    {
        return Success(await _sysJobService.StopAsync(id!.Value));
    }
    
    [VinAuthorize("monitor:job:query"), HttpGet("record-list")]
    public async Task<IActionResult> List([FromQuery] SysJobRecordListDto dto)
    {
        return Success(await _sysJobRecordService.ListAsync(dto), dto.TotalCount);
    }

    [VinAuthorize("monitor:job:query"), HttpGet("record-detail")]
    public async Task<IActionResult> RecordDetail(long id)
    {
        return Success(await _sysJobRecordService.QueryAsync(id));
    }

    #endregion

    #region 任务分组

    [VinAuthorize("monitor:job:list"), HttpGet("group-list")]
    public async Task<IActionResult> GroupList()
    {
        return Success(await _sysJobGroupService.ListAllAsync());
    }

    #endregion
}